package edu.ustb.gateway.service;

import com.google.gson.Gson;
import edu.ustb.gateway.entity.InputMessageMq;
import edu.ustb.gateway.mapper.InputMessageMqMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;

/**
 * @author ShiZhe
 * @create 2021-04-22 22:15
 */
@Component
@EnableBinding(Sink.class)
public class GatewayServiceInput {

    @Autowired
    private InputMessageMqMapper inputMessageMqMapper;

    @Value("${server.port}")
    private String serverPort;

    Gson gson = new Gson();
    public InputMessageMq inputMessageMq;

    @StreamListener(Sink.INPUT)
    public void input(String json) {
        inputMessageMq = gson.fromJson(json, InputMessageMq.class);
        System.out.println("消费者" + serverPort + "号，收到消息：" + json);
        System.out.println(inputMessageMq.toString());
        inputMessageMqMapper.insert(inputMessageMq);
    }
}

// 返回的消息的json格式
//{
//        "responseId":1,
//        "callbackUrl":"gatewayExchange",
//        "result":200,
//        "content":"123"
//}